var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var AppList = require('modules/list/like_lagou');
var api = libs.api;
var valide = libs.formValide;

// var _kkk = {
//     "actionUrl":"/imapi/h5/index.html?login=b4b1be09e3584df08925a457823159d5&messageId=42f04371543d4cd8b1c7b8a55fd6ca21#!/communityNotice",
//     "bizType":"COMMUNITY_BULLETIN",
//     "channelType":"1",
//     "createBy":"1",
//     "createTime":"2016-04-28 14:20:19",
//     "createTimeStr":"2016-04-01 00:45",
//     "errorCode":"",
//     "isDeleted":false,
//     "isUsed":false,
//     "messageId":"42f04371543d4cd8b1c7b8a55fd6ca21",
//     "msgPushRecordId":"1",
//     "pushNumber":0,
//     "pushType":"APP",
//     "receivedAcct":"1",
//     "receivedBy":"05354d9ba36643378bb7c292cc2d52f1",
//     "status":"SUCCEED",
//     "title":"软件更新哈哈",
//     "updateTime":null
// }
//
// var __data = {
//     "beginPageIndex":0,
//     "countResultMap":null,
//     "currentPage":1,
//     "endPageIndex":0,
//     "numPerPage":10,
//     "pageCount":0,
//     "recordList":[
//         {
//             "actionUrl":"/imapi/h5/index.html?login=b4b1be09e3584df08925a457823159d5&messageId=42f04371543d4cd8b1c7b8a55fd6ca21#!/communityNotice",
//             "bizType":"COMMUNITY_BULLETIN",
//             "channelType":"1",
//             "createBy":"1",
//             "createTime":"2016-04-28 14:20:19",
//             "createTimeStr":"2016-04-01 00:45",
//             "errorCode":"",
//             "isDeleted":false,
//             "isUsed":false,
//             "messageId":"42f04371543d4cd8b1c7b8a55fd6ca21",
//             "msgPushRecordId":"1",
//             "pushNumber":0,
//             "pushType":"APP",
//             "receivedAcct":"1",
//             "receivedBy":"05354d9ba36643378bb7c292cc2d52f1",
//             "status":"SUCCEED",
//             "title":"软件更新哈哈",
//             "updateTime":null
//         }
//     ],
//     "totalCount":1
// }
//
// for (var i=0; i<30; i++){
//     __data.recordList.push(_kkk)
// }


//当前页公共变量
var _page = {}
var _list_msg = [];
var _pageNum = 1;


var bindEvent = function(){

    //加载下一页数据
    function loadMore(e, next){
        pull_list_data(function(){
            next(e, _list_msg)
        })
    }

    function itemFun(){
        $(this).click(function(){
            var messageId = $(this).attr('data-id');
            sessionStorage.setItem('messageId',messageId);
            router('uc/messagedetail');
        });
    }

    var _empty = <div className="uc-message-empty" id="uc-message-empty" style={{display:'block'}}>
        <div>暂无消息通知记录</div>
    </div>

    function initDate(){
        pull_list_data(function(){
            AppList(_list_msg, {
                container: 'uc-message',
                globalName: 'UcMessage',
                listClass: 'uc-message-list',
                itemMethod: itemFun,
                scrollEnd: loadMore,
                empty: _empty
            })
        })
    }

    var _user = SA.get('USER')
    if (_user.login){
        initDate()
    }
    else {
        SA.set('USER', initDate)
    }

};

//演示模块
var Message = React.createClass({
    mixins: [ItemMixin],
    render:function(){
        return (
            <div className="uc-message flex flex-v">
                <div id="uc-message" className="flex-1 f-oTouch1"> </div>
            </div>
        );
    }
});

function timeFormat(time){
    // var timeStr = new Date(time).getTime();
    var arr = time.split(/[- :]/),
        _date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]),
        timeStr = Date.parse(_date)

    return libs.timeAgo(timeStr, function(t){
        if (t.diff.year>=1){
            return _date.Format("yyyy-MM-dd")
        }
        else
        if (t.diff.month>=1 && t.diff.month<12){
            return _date.Format("yyyy-MM-dd")
        }
        else
        if (t.day>=0 && t.diff.day<30){
            if (t.day>=0&&t.day<1){
                var _time = _date.Format("hh:mm")
                return '昨天 '+_time
            }
            else
            if (t.day>=1&&t.day<=2){
                var _time = _date.Format("hh:mm")
                return '前天 ' + _time
            }
            else
                return _date.Format("yyyy-MM-dd hh:mm")
        }
        else
        if (t.diff.hour>=1 && t.diff.hour<24){
            if (t.day<0){
                return _date.Format("hh:mm")
            }
        }
        else
        if (t.diff.minute>=1 && t.diff.minute<60){
            return _date.Format("hh:mm")
        }
        else
        if (t.diff.seconds>=1 && t.diff.seconds<60){
            return _date.Format("hh:mm")
        }
    });
}

function pull_list_data(cb){
    var login = SA.get('USER').login;
    // pageNum, numPerPage, order
    var params = {
        pageNum: _pageNum,
        numPerPage: 20,
        method:'get',
        login:login
    };
    api.req('/messageList',params,function(data){
        if (!data.code){
            _pageNum = data.currentPage+1
            if (data.recordList){
                _list_msg = [];
                data.recordList.map(function(item,index){
                    var _cls = 'noclass'
                    if (!item.isUsed){
                        _cls = 'active'
                    }
                    _list_msg.push({
                        className: _cls,
                        "data-id": item.messageId,
                        title: <a><div>{item.title}</div><div>{timeFormat(item.createTime)}</div></a>
                    })
                })
                if (cb && typeof cb==='function'){
                    cb()
                }
            }
        }
    })
}



function start(name){

    return pages.new({
        trigger:function(){
            this.libs.changeTitle('消息中心');    //更改当前页面标题
            this.main()
        },
        ready: function(){
        },
        main: function(self ,data){
            this.render(
                <Message itemDefaultMethod={bindEvent} />,
                document.getElementById(name)
            )
        },
        end: function(){
            _pageNum = 1;
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
}

module.exports = start;
